const edge = require('edge-js')

let typeName = 'TSCSDK.node_comport'
function edgeFunc(methodName) {
  return edge.func({
    assemblyFile: 'tsc-printer/tsclibnet.dll',
    typeName,
    methodName
  })
}
function setPrintContent() {
  edgeFunc('clearbuffer')('', true)
  edgeFunc('printerfont')({ x: '0', y: '0', fonttype: '3', rotation: '0', xmul: '1', ymul: '1', text: 'Font Test二货' }, true)
  edgeFunc('barcode')({ x: '50', y: '100', type: '128', height: '70', readable: '0', rotation: '0', narrow: '3', wide: '1', code: '123456' }, true)
  edgeFunc('windowsfont')({ x: 0, y: 200, fontheight: 32, rotation: 0, fontstyle: 0, fontunderline: 0, szFaceName: 'KUAI', content: 'Windowsfont 煞笔' }, true)
  // edgeFunc('sendcommand')('CODEPAGE UTF-8', true)
  // edgeFunc('sendcommand')('TEXT 250,50,\"0\",0,10,10,\"中午Text Test!!\"', true)
  // edgeFunc('sendcommand_utf8')('TEXT 0,50,\"ROMAN.TTF\",0,10,10,\"why測試中文!!\"', true)
  edgeFunc('sendcommand_utf8')(`QRCODE 100,230,H,5,A,0,M2,"%7B%22sku%22%3A%221%22%2C%22color%22%3A%22%E9%BB%91%22%2C%22size%22%3A%22175%2F80A%2F31%22%2C%22price%22%3A459%7D"`, true)
  edgeFunc('printlabel')({ quantity: '1', copy: '1' }, true)

  // var selftest_command = 'QRCODE 50"12345"\r\n';
  // var arr = [];
  // for (var i = 0; i < selftest_command.length; ++i) 
  //    arr.push(selftest_command.charCodeAt(i));
  // var selftest_command_buffer = new Uint8Array(arr);
  // edgeFunc('sendcommand_binary')(selftest_command_buffer, true);
}
const fontstyle = {
  'Normal': 0,
  'Italic': 1,
  'Bold': 2,
  'Bold and Italic': 3
}
function myContent(data) {
  edgeFunc('clearbuffer')('', true)
  edgeFunc('sendcommand')('DIRECTION 1,0')
  // edgeFunc('sendcommand')('SIZE 40 mm,93 mm')
  // edgeFunc('sendcommand')('GAP 3 mm')
  // 送料长度撕纸用
  // edgeFunc('sendcommand')('OFFSET 30 mm')
  // 正值使标签远离印刷方向;负值将使标签朝打印方向移动
  edgeFunc('sendcommand')('SHIFT -40')
  for (let item of data.list) {
    if (item.type === 'text') {
      // console.log(item)
      edgeFunc('windowsfont')({ x: Math.round(item.x * 8), y: Math.round(item.y * 8), fontheight: Math.round(item.fontheight * 8), rotation: 0, fontstyle: fontstyle[item.fontstyle], fontunderline: 0, szFaceName: 'SimHei', content: item.value }, true)
    } else if (item.type === 'barcode') {
      edgeFunc('barcode')({ x: (item.x * 8).toFixed(), y: (item.y * 8).toFixed(), type: '128', height: (item.height * 8).toFixed(), readable: '2', rotation: '0', narrow: '2', wide: '1', code: item.value }, true)
    }
  }
  edgeFunc('printlabel')({ quantity: '1', copy: data.copy.toString() }, true)
}

function comport(){
  typeName = 'TSCSDK.node_comport'
  edgeFunc('openport')({ portnumber: 'COM6', baudrate: '9600', parity: 'N', databit: '8', stopbit: '1' }, true)
  setPrintContent()
  edgeFunc('closeport')(1000, true)
}
function usb() {
  typeName = 'TSCSDK.node_usb'
  edgeFunc('openport')('')
  myContent({"list":[{"fontstyle":"Bold","x":7,"y":0,"type":"text","value":"合格证","fontheight":4},{"fontstyle":"Normal","x":3,"y":5,"label":"品名","type":"text","value":"品名：精品上衣","fontheight":2.5},{"fontstyle":"Normal","x":3,"y":9,"label":"款号","type":"text","value":"款号：2175A","fontheight":2.5},{"fontstyle":"Normal","x":3,"y":13,"label":"尺码","type":"text","value":"尺码：170/92A/XL","fontheight":2.5},{"fontstyle":"Normal","x":3,"y":17,"label":"颜色","type":"text","value":"颜色：黑色","fontheight":2.5},{"fontstyle":"Normal","x":3,"y":20.8,"label":"等级","type":"text","value":"等级：一等品","fontheight":2.5},{"fontstyle":"Normal","x":2.4,"y":25.6,"label":"执行标准","type":"text","value":"执行标准：FZ/T81004-2012","fontheight":2.5},{"fontstyle":"Normal","x":1.6,"y":30.6,"label":"安全类别","type":"text","value":"安全类别：GB18401-2010 B类","fontheight":2.5},{"x":9.9,"y":37.3,"type":"text","label":"","value":"氨纶：60%","fontstyle":"Normal","fontheight":2.5},{"fontstyle":"Normal","x":2.4,"y":34.8,"label":"面料","type":"text","value":"真丝：40%","fontheight":2.5},{"x":3,"y":42.4,"type":"barcode","value":"2175A","height":6}],"copy":1})
  edgeFunc('closeport')('', true)
}
function driver() {
  let printerName = 'TSC TTP-244 Plus'
  typeName = 'TSCSDK.node_driver'
  edgeFunc('openport')(printerName, true)
  myContent({"list":[{"fontstyle":"Bold","x":7,"y":0,"type":"text","value":"合格证","fontheight":4},{"fontstyle":"Normal","x":3,"y":5,"label":"品名","type":"text","value":"品名：精品上衣","fontheight":2.5},{"fontstyle":"Normal","x":3,"y":9,"label":"款号","type":"text","value":"款号：2175A","fontheight":2.5},{"fontstyle":"Normal","x":3,"y":13,"label":"尺码","type":"text","value":"尺码：170/92A/XL","fontheight":2.5},{"fontstyle":"Normal","x":3,"y":17,"label":"颜色","type":"text","value":"颜色：黑色","fontheight":2.5},{"fontstyle":"Normal","x":3,"y":20.8,"label":"等级","type":"text","value":"等级：一等品","fontheight":2.5},{"fontstyle":"Normal","x":2.4,"y":25.6,"label":"执行标准","type":"text","value":"执行标准：FZ/T81004-2012","fontheight":2.5},{"fontstyle":"Normal","x":1.6,"y":30.6,"label":"安全类别","type":"text","value":"安全类别：GB18401-2010 B类","fontheight":2.5},{"x":9.9,"y":37.3,"type":"text","label":"","value":"氨纶：60%","fontstyle":"Normal","fontheight":2.5},{"fontstyle":"Normal","x":2.4,"y":34.8,"label":"面料","type":"text","value":"真丝：40%","fontheight":2.5},{"x":3,"y":42.4,"type":"barcode","value":"2175A","height":6}],"copy":2})
  edgeFunc('closeport')('', true)
}
function ethernet() {
  typeName = 'TSCSDK.node_ethernet'
  edgeFunc('openport')({ ipaddress: '10.0.10.172', port: '9100', delay: '500' }, true)
  setPrintContent()
  edgeFunc('closeport')(2000, true)
}

usb()